package com.hina.rec.endpoint.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import com.hina.rec.application.service.system.MenuService;
import com.hina.rec.common.model.Result;
import com.hina.rec.common.model.IdReq;
import com.hina.rec.endpoint.req.system.MenuSaveReq;
import com.hina.rec.endpoint.req.system.MenuUpdateReq;
import com.hina.rec.endpoint.res.system.MenuRes;
import com.hina.rec.endpoint.res.system.MenuTreeRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单控制器
 *
 * @author liaosp
 * @date 2024-01-10
 */
@Tag(name = "menu", description = "菜单管理相关接口")
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@SaIgnore
public class MenuController {

    private final MenuService menuService;

    /**
     * 创建菜单
     *
     * @param req 菜单信息
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:menu:edit")
    @PostMapping("/save")
    @Operation(summary = "创建菜单", description = "创建菜单")
    public Result<Boolean> save(@RequestBody @Valid MenuSaveReq req) {
        boolean result = menuService.save(req);
        return Result.success(result);
    }

    /**
     * 更新菜单
     *
     * @param req 菜单信息
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:menu:edit")
    @PostMapping("/update")
    @Operation(summary = "更新菜单", description = "更新菜单")
    public Result<Boolean> update(@RequestBody @Valid MenuUpdateReq req) {
        boolean result = menuService.update(req);
        return Result.success(result);
    }

    /**
     * 删除菜单
     *
     * @param req id
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:menu:edit")
    @PostMapping("/delete")
    @Operation(summary = "删除菜单", description = "根据菜单ID删除菜单")
    public Result<Boolean> delete(@RequestBody @Valid IdReq req) {
        boolean result = menuService.delete(req.getId());
        return Result.success(result);
    }

    /**
     * 获取菜单列表
     *
     * @return 菜单列表
     */
    @SaCheckPermission(value = "system:menu:query")
    @PostMapping("/list")
    @Operation(summary = "获取菜单列表", description = "获取菜单列表(用于【菜单管理】界面)")
    public Result<List<MenuRes>> list() {
        return Result.success(menuService.list());
    }

    /**
     * 菜单详情
     *
     * @param req 请求参数
     * @return 菜单详情
     */
    @SaCheckPermission(value = "system:menu:query")
    @PostMapping("/detail")
    @Operation(summary = "获取菜单详情", description = "根据菜单ID获取菜单详情")
    public Result<MenuRes> detail(@RequestBody @Valid IdReq req) {
        return Result.success(menuService.detail(req));
    }

    /**
     * 获取菜单树
     *
     * @return 菜单树
     */
    @SaCheckPermission(value = {"system:menu:query", "system:role:query"}, mode = SaMode.OR)
    @PostMapping("/tree")
    @Operation(summary = "获取菜单树", description = "获得菜单树")
    public Result<List<MenuTreeRes>> tree() {
        return Result.success(menuService.tree());
    }
}